package com.fancy.learncenter.view;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.EducationDataBean;
import com.fancy.learncenter.bean.RewardDataBean;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.superservice.lya.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hyy on 2018/2/25.
 * Explain activity as
 */

public class PopupWindowRewardType extends BasedPopupWindow {

    View locationView;

    View popopView;
    View viewParent;

    RecyclerView recyclerView;

    Activity mContext;

    TextView confrim;
    TextView title_tv;

    public PopupWindowRewardType(Activity mContext, View viewParent) {
        super(mContext);
        this.mContext = mContext;
        this.viewParent = viewParent;
        this.locationView = locationView;
        popopView = LayoutInflater.from(MyApplication.context).inflate(R.layout.pupopwinodw_salary, null);
        initPopupWionw();

        getDatas();
    }

    private void initPopupWionw() {
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//        this.setOutsideTouchable(true);

        recyclerView = (RecyclerView) popopView.findViewById(R.id.recycleView);
        confrim = popopView.findViewById(R.id.confrim);
        title_tv = popopView.findViewById(R.id.title_tv);
        title_tv.setText("公司福利");
        confrim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder stringBuilderName = new StringBuilder();
                StringBuilder stringBuilderId = new StringBuilder();
                for (int i = 0; i < itemDatas.size(); i++) {

                    if (itemDatas.get(i).isSelected()) {
                        stringBuilderName.append(itemDatas.get(i).getWelfare_name() + "  ");
                        stringBuilderId.append(itemDatas.get(i).getId() + ",");
                    }
                }
                dismiss();
                if (clickCallBack != null) {
                    if (!TextUtils.isEmpty(stringBuilderId.toString())) {
                        clickCallBack.selecCallBack(stringBuilderName.toString(), stringBuilderId.toString().substring(0, stringBuilderId.toString().length() - 1) + "");
                    }

                }

            }
        });

        GridLayoutManager linearLayoutManager = new GridLayoutManager(mContext, 3);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);


        popopView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupWindowRewardType.this.dismiss();
            }
        });

        this.update();

        this.setContentView(popopView);

        commonRecycleViewAdapter = new CommonRecycleViewAdapter<RewardDataBean>(mContext, R.layout.pupopwinodw_multi_text_item, new ArrayList<RewardDataBean>()) {
            @Override
            public void bindView(CustomViewHold customViewHold, final RewardDataBean item, final int position) {
                final TextView content = customViewHold.getView(R.id.content);
                content.setText(item.getWelfare_name());

                if (item.isSelected()) {
                    content.setBackgroundResource(R.color.orange);
                    content.setTextColor(Color.parseColor("#FFFFFF"));
                } else {
                    content.setBackgroundResource(R.color.white);
                    content.setTextColor(Color.parseColor("#3f4344"));
                }

                content.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.isSelected()) {
                            item.setSelected(false);
                        } else {
                            item.setSelected(true);
                        }
                        notifyItemChanged(position);
                    }
                });
            }
        };
        recyclerView.setAdapter(commonRecycleViewAdapter);

    }

    public EducationDataBean cutItem;
    private int oldSelect = 0;

    @Override
    public void showPopupWindow() {
        super.showPopupWindow();
        showAsDropDown(viewParent);
    }

    public void setClickCallBack(PopupWindowEducationType.ClickCallBack clickCallBack) {
        this.clickCallBack = clickCallBack;

    }

    private CommonRecycleViewAdapter<RewardDataBean> commonRecycleViewAdapter;

    @Override
    public void dismiss() {
        super.dismiss();
        if (clickCallBack != null) {
            clickCallBack.cancleCallBack();
        }
    }

    private PopupWindowEducationType.ClickCallBack clickCallBack;

    public interface ClickCallBack {
        abstract void cancleCallBack();

        abstract void selecCallBack(String content, String id);
    }

    List<RewardDataBean> itemDatas;

    private void getDatas() {
        Map map = new HashMap();
        HttpMehtod.getInstance().getRewardType(map, new IdeaObserver<BaseDataBean<List<RewardDataBean>>>() {

            @Override
            public void onSuccess(BaseDataBean<List<RewardDataBean>> listBaseDataBean) {

                itemDatas = listBaseDataBean.getData();
                commonRecycleViewAdapter.notifyDataSetChanged((ArrayList<RewardDataBean>) itemDatas);
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }
}
